//
//  XJSCGoodsClvProxy.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/26.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  收藏的商品的collectionview的代理方法

#import "XJSCGoodsClvProxy.h"

//veiw
#import "XJShouCangGoodsItem.h"
//model
#import "XJShouCangGoodsModel.h"

@implementation XJSCGoodsClvProxy

#pragma mark - 一 UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //cell 的注册 在控制器中进行
    XJShouCangGoodsItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"XJShouCangGoodsItem" forIndexPath:indexPath];
    
    //赋值
    XJShouCangGoodsModel *goodsM = self.dataArray[indexPath.row];
    item.model = goodsM;
    
    //显隐选择按钮
    [item xjscgoodsItem_showOrHideSelectedBtnWithState:self.isScgoodsClvEditing];
//    [collectionView reloadData]; //1.没用。2.不能在控制器中同时调用 直接奔溃。
    
    return item;
}

#pragma mark - 一 UICollectionViewDelegateFlowLayout
//size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat w = (KWidth - 12) / 2;
    CGFloat h = (UI_IS_IPHONE5 ? ADAPTHeight(280) : ADAPTHeight(260));

    return CGSizeMake(w, h);
}

#pragma mark - X间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 6;
}
#pragma mark - Y间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 4;
}

#pragma mark - 一 UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    XJShouCangGoodsModel *model = self.dataArray[indexPath.row];
    
    //更新界面
    if (self.isScgoodsClvEditing) {
        [collectionView deselectItemAtIndexPath:indexPath animated:YES];
        model.isScgoosModelSelected = !model.isScgoosModelSelected;
        
        [collectionView reloadData]; //✅ 关键代码
    }
    
    //回调
    if (self.xjscgoodsclvProxySelecteCellBlock) {
        self.xjscgoodsclvProxySelecteCellBlock(indexPath, self.isScgoodsClvEditing, model.isScgoosModelSelected, model);
        
//        self.xjscgoodsclvProxySelecteCellBlock(indexPath, self.isScgoodsClvEditing, model.isScgoosModelSelected);
    }
}


@end
